//
//  UIViewController+Navigation.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/3/28.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "UIViewController+Navigation.h"
#import "GUIButton.h"

@implementation UIViewController (Navigation)

- (void)setNavigationBarBackgroundImage:(NSString *)imageName {
    if (nil == imageName || [@"" isEqualToString:imageName]) {
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:imageName] forBarMetrics:UIBarMetricsDefault];
    }
}

- (void)setNavigationBarBackgroundWithImage:(UIImage *)image {
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

- (void)setNavigationBarShadowImge:(UIImage *) image {
    [self.navigationController.navigationBar setShadowImage:image];
}

- (void)setNavigationBarBackgroundColor:(UIColor *) colorName {
    if (nil == colorName) {
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    } else {
        self.navigationController.navigationBar.barTintColor = colorName;
    }
}

- (id)setTextTitleViewWithFrame:(CGRect)frame
                         title:(NSString *)titile
                      fontSize:(int)size
                     textColor:(UIColor *) color {
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = titile;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = color;
    label.font = [UIFont systemFontOfSize:18];
    [label sizeToFit];
    self.navigationItem.titleView = label;
    return label;
}

- (id)setImageTitleViewWithFrame:(CGRect)frame
                          image:(NSString *)image {
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.image = [UIImage imageNamed:image];
    imageView.contentMode = UIViewContentModeCenter;
    self.navigationItem.titleView = imageView;
    return imageView;
}
- (id)setSegmentTitleViewWithItems:(NSArray *)items {
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:items];
    segment.selectedSegmentIndex = 0;
    self.navigationItem.titleView = segment;
    return segment;
}
- (id)setViewTitleView:(UIView *) view {
    self.navigationItem.titleView = view;
    return view;
}
- (id)setLeftBarButtonItemWithFrame:(CGRect)frame
                              title:(NSString *)titile
                         titleColor:(UIColor *)titleColor
                              image:(NSString *)image
                          backImage:(NSString *)backImage
                    selectBackImage:(NSString *)selectBackImage
                             action:(void(^)(GUIButton *button))action {
    GUIButton * leftButton = [GUIButton buttonWithType:UIButtonTypeCustom];
    if (frame.size.width == 0) {
        leftButton.frame = CGRectMake(0, 0, 60, 44);
    } else {
        leftButton.frame = frame;
    }
    [leftButton setBackgroundImage:[UIImage imageNamed:backImage] forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:selectBackImage] forState:UIControlStateSelected];
    [leftButton setTitle:titile forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [leftButton setTitleColor:titleColor forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    return leftButton;
}
-(id)setLeftImageBarButtonItemWithFrame:(CGRect)frame
                                  image:(NSString *)image
                            selectImage:(NSString *)selectImage
                                 action:(void(^)(GUIButton *button))action {
    GUIButton * leftButton = [GUIButton buttonWithType:UIButtonTypeCustom];
    if (frame.size.width == 0) {
        leftButton.frame = CGRectMake(0, 0, 60, 44);
        leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, +20);
    } else {
        leftButton.frame = frame;
    }
    leftButton.action = action;
    [leftButton setImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];
    [leftButton setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    return leftButton;
}
- (id)setLeftTextBarButtonItemWithFrame:(CGRect)frame
                                  title:(NSString *)titile
                             titleColor:(UIColor *)titleColor
                              backImage:(NSString *)backImage
                        selectBackImage:(NSString *)selectBackImage
                                 action:(void(^)(GUIButton *button))action {
    GUIButton * leftButton = [GUIButton buttonWithType:UIButtonTypeCustom];
    if (frame.size.width == 0) {
        leftButton.frame = CGRectMake(0, 0, 60, 44);
    } else {
        leftButton.frame = frame;
    }
    [leftButton setTitle:titile forState:UIControlStateNormal];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [leftButton setTitleColor:titleColor forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:backImage] forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:selectBackImage] forState:UIControlStateSelected];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    return leftButton;
}

- (id)setRightBarButtonItemWithFrame:(CGRect)frame title:(NSString *)titile titleColor:(UIColor *)titleColor image:(NSString *)image backImage:(NSString *)backImage selectBackImage:(NSString *)selectBackImage action:(void (^)(GUIButton *))action {
    GUIButton * rightButton = [GUIButton buttonWithType:UIButtonTypeCustom];
    if (frame.size.width == 0) {
        rightButton.frame = CGRectMake(0, 0, 60, 44);
    } else {
        rightButton.frame = frame;
    }
    [rightButton setBackgroundImage:[UIImage imageNamed:backImage] forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:selectBackImage] forState:UIControlStateSelected];
    [rightButton setTitle:titile forState:UIControlStateNormal];
    [rightButton setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [rightButton setTitleColor:titleColor forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = leftBarButtonItem;
    return rightButton;
}
- (id)setRightImageBarButtonItemWithFrame:(CGRect)frame image:(NSString *)image selectImage:(NSString *)selectImage ishidden:(BOOL)hidden action:(void (^)(GUIButton *))action {
    GUIButton * rightButton = [GUIButton buttonWithType:UIButtonTypeCustom];
    if (frame.size.width == 0) {
        rightButton.frame = CGRectMake(0, 0, 60, 44);
        rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, - (60 - rightButton.imageView.size.width) / 2.);
    } else {
        rightButton.frame = frame;
    }
    UIView * pointView;
    if (hidden) {
        
    } else {
        pointView = [[UIView alloc] initWithFrame:CGRectMake(rightButton.frame.size.width - 4, 0, 4, 4)];
        pointView.layer.masksToBounds = YES;
        pointView.layer.cornerRadius = 2;
        pointView.backgroundColor = [UIColor redColor];
        [rightButton addSubview:pointView];
        
    }
    rightButton.action = action;
    [rightButton setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [rightButton setImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = leftBarButtonItem;
    return rightButton;
}

- (id)setRightTextBarButtonItemWithFrame:(CGRect)frame title:(NSString *)titile titleColor:(UIColor *)titleColor backImage:(NSString *)backImage selectBackImage:(NSString *)selectBackImage action:(void (^)(GUIButton *))action {
    GUIButton * rightButton = [GUIButton buttonWithType:UIButtonTypeCustom];
    if (frame.size.width == 0) {
        rightButton.frame = CGRectMake(0, 0, 60, 44);
    } else {
        rightButton.frame = frame;
    }
    rightButton.action = action;
    [rightButton setBackgroundImage:[UIImage imageNamed:backImage] forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:selectBackImage] forState:UIControlStateSelected];
    [rightButton setTitle:titile forState:UIControlStateNormal];
    [rightButton setTitleColor:titleColor forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = leftBarButtonItem;
    return rightButton;
}

-(id)setRightImageBarButtonItemWithImageArray:(NSArray *)array action:(void(^)(GUIButton *button))action {
    
    NSMutableArray * arr = [NSMutableArray array];
    for (int i = 0; i < array.count; i++) {
        GUIButton *rightButton = [GUIButton buttonWithType:UIButtonTypeCustom];
        [rightButton setImage:[UIImage imageNamed:array[i]] forState:UIControlStateNormal];
        rightButton.tag = i;
        rightButton.action = action;
        UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        rightButton.frame = CGRectMake(44 * i, 0, 30, 44);
        rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 7.5, 0, -7.5);
        [arr addObject:rightBarButtonItem];
    }
    self.navigationItem.rightBarButtonItems = arr;
    return arr;
}


@end
